package com.yanqu.road.server.manger.activity.zhanling;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.zhanling.ZhanLingActivityModule;
import com.yanqu.road.server.gameplayer.module.player.RechargeModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.config.MallRechargeConfigMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author ：zfr
 * @date ：2021/3/24 10:06
 */
public class ZhanLingActivityMgr extends TempMgr {
    private static Logger logger = LogManager.getLogger(ZhanLingActivityMgr.class.getName());
    public static int MeiLiScore = 101946;
    public static int QinMiScore = 101947;
    public static int MenKeScore = 101948;
    public static int ZhuanSuScore = 101949;
    public static int YuanBaoScore = 101950;

    public static void reloadActivityInfo() {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getZhanLingActivityInfoList();
        if (activityInfoList != null && activityInfoList.size() > 0) {
            playerReloadActivity();
        }
        logger.info("reload zhanLing activity end");
    }

    private static void playerReloadActivity() {
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        for (GamePlayer onlinePlay : playerList) {
            onlinePlay.getModule(ZhanLingActivityModule.class).syncActivity();
        }
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    /**
     * 获取玩家触发的礼包id
     * @param activityId
     * @param player
     * @return
     */
    public static List<Integer> getUserTriggerGift(int activityId, GamePlayer player) {
        List<Integer> triggerGiftIds = new ArrayList<>();
        List<MallInfo> mallInfos = new ArrayList<>();
        //玩家近7日内充值成功的最大档位
        long price = player.getModule(RechargeModule.class).lastSevenDayMaxRechargeLv();
        List<ActivityConditionInfo> conditionInfoList = NormalActivityMgr.getZhanLingActivityConditionInfoList(activityId);
        for (ActivityConditionInfo conditionInfo : conditionInfoList) {
            MallInfo mallInfo = MallMgr.getMallInfoByMallId(conditionInfo.getParamList().get(2).intValue());
            if (mallInfo != null) {
                mallInfos.add(mallInfo);
            }
        }
        //根据玩家7日内的充值匹配档位
//        mallInfos.sort(Comparator.comparing(o -> Long.parseLong(o.getPrice())));
        mallInfos.sort(Comparator.comparing(MallRechargeConfigMgr::getPrice));
        int mallId = -1;
        for (MallInfo mallInfo : mallInfos) {
            long checkPrice = MallRechargeConfigMgr.getPrice(mallInfo);
            if (mallId == -1 || checkPrice <= price) {
                mallId = mallInfo.getMallId();
            }
        }
        if (mallId != -1) {
            triggerGiftIds.add(mallId);
        }
        return triggerGiftIds;
    }

    /**
     * 是否有该礼包的活动进行中
     * @param mallId
     * @return
     */
    public static boolean mallActivityIsInTime(int mallId) {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getZhanLingActivityInfoList();
        for (ActivityInfo activityInfo : activityInfoList) {
            List<ActivityConditionInfo> conditionInfoList = NormalActivityMgr.getZhanLingActivityConditionInfoList(activityInfo.getActivityId());
            for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                if (conditionInfo.getParamList().get(2).intValue() == mallId) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 是否是战令积分
     * @param itemId
     * @return
     */
    public static boolean isZhanLingScore(int itemId) {
        return itemId == MeiLiScore || itemId == QinMiScore || itemId == MenKeScore || itemId == ZhuanSuScore || itemId == YuanBaoScore;
    }

    public static boolean isZhanLingGiftCondition(int conditionType) {
        return conditionType == eGamePlayerEventType.ZhanLingMeiLiGiftBag.getValue()
                || conditionType == eGamePlayerEventType.ZhanLingQinMiGiftBag.getValue()
                || conditionType == eGamePlayerEventType.ZhanLingMenKeGiftBag.getValue()
                || conditionType == eGamePlayerEventType.ZhanLingZhuanSuGiftBag.getValue()
                || conditionType == eGamePlayerEventType.ZhanLingYuanBaoGiftBag.getValue();
    }
}
